package com.ly.ocw.web.admin;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ly.ocw.entity.weixin.WeixinCard;
import com.ly.ocw.entity.weixin.WeixinUserCard;
import com.ly.ocw.exception.ServerException;
import com.ly.ocw.service.weixin.WeixinAccessTokenService;
import com.ly.ocw.service.weixin.WeixinCardService;
import com.ly.ocw.service.weixin.WeixinUserCardService;
import com.ly.ocw.vo.wxcard.response.CreateCardResponse;

@Controller
@RequestMapping(value = "/admin/card")
public class AdminCardController {

	private static Logger logger = LoggerFactory.getLogger(AdminCardController.class);

	@Autowired
	private WeixinCardService weixinCardService;

	@Autowired
	private WeixinUserCardService weixinUserCardService;

	@Autowired
	private WeixinAccessTokenService weixinAccessTokenService;

	// http://wx.wonderlink.com.cn:80/boycard/admin/card/list
	// to card list
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {

		List<WeixinCard> cardList = weixinCardService.getAllCards();

		logger.info("admin/card/list size :" + cardList.size());

		model.addAttribute("cardList", cardList);

		return "admin/card/cardList";
	}

	// to card list
	@RequestMapping(value = "cardUserList/{cardId}", method = RequestMethod.GET)
	public String list(@PathVariable("cardId") String cardId, Model model) {

		List<WeixinUserCard> userCardList = weixinUserCardService.getByCardId(cardId);

		logger.info("admin/card/cardUserList size :" + userCardList.size());

		model.addAttribute("userCardList", userCardList);

		return "admin/card/cardUserList";
	}

	// 在微信端创建企业会员卡（调微信服务器接口），创建之后可以通过多种方式让用户领取会员卡，并将会员卡添加到微信卡包
	@RequestMapping(value = "createCard", method = RequestMethod.GET)
	@ResponseBody
	public CreateCardResponse getCreateCard() throws ClientProtocolException, ServerException, IOException {
		logger.info("Cart");

		CreateCardResponse createCardResponse = weixinCardService.createVipCard();

		return createCardResponse;
	}

	// 查询会员卡审核状态（创建会员卡后需要微信审核通过后才能发卡给用户）
	// card_id = pMBADj7D5ke257fPW0YL2OpZN_3A
	// card_id = pMBADj4nloPYJPIv5yC8M6YvO8Do
	@RequestMapping(value = "info/{cardId}", method = RequestMethod.GET)
	@ResponseBody
	public String getCardInfo(@PathVariable("cardId") String cardId) throws ClientProtocolException, ServerException, IOException {
		logger.info("Cart");

		String str = weixinCardService.getCardInfo(cardId);

		return str;
	}

	/**
	 * 手工激活会员卡
	 * 
	 * @param model
	 * @return
	 * @throws ServerException
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	@RequestMapping(value = "activate", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String activateCard(HttpServletRequest request, HttpServletResponse response, Model model) throws ClientProtocolException, IOException, ServerException {
		String card_id = request.getParameter("card_id");
		String openid = request.getParameter("openid");
		logger.info("RequestBody:" + card_id);
		logger.info("RequestInfo:{RemoteAddr:" + request.getRemoteAddr() + ",LocalName:" + request.getLocalName() + ",LocalPort:" + request.getLocalPort() + ",Method"
				+ request.getMethod() + ",PathInfo:" + request.getPathInfo() + ",QueryString:" + request.getQueryString() + ",RemoteAddr:" + request.getRemoteAddr()
				+ ",RemoteHost:" + request.getRemoteHost() + ",RemotePort:" + request.getRemotePort() + ",RequestURI:" + request.getRequestURI() + "}");

		Enumeration<String> pnameEnu = request.getParameterNames();
		logger.info("----- 参数Parameters Start ----");
		while (pnameEnu.hasMoreElements()) {

			logger.info("{" + pnameEnu.nextElement() + ":" + request.getParameter(pnameEnu.nextElement()) + "}");

		}
		logger.info("----- 参数Parameters End ----");

		WeixinUserCard weixinUserCard = weixinUserCardService.getByOpenid(openid);
		String respStr = weixinCardService.activateCard(card_id, weixinUserCard.getCardNumber(),openid);
		return respStr;
	}

	// 删除一类卡券
	@RequestMapping(value = "delete/{cardId}", method = RequestMethod.GET)
	@ResponseBody
	public String deleteCard(@PathVariable("cardId") String cardId) throws ClientProtocolException, ServerException, IOException {
		logger.info("deleteCard");

		String str = weixinCardService.deleteCard(cardId);
		logger.info("deleteCard result:" + str);
		return str;
	}

}
